package moodish.client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import moodish.client.MessageHendler;
import moodish.client.MoodishMessage;
import moodish.client.gui.listener.RepaintListener;
import moodish.client.gui.listener.Sentinel;
import moodish.client.user.User;
import moodish.comm.ClientComm;

public class MoodishClientGui {

	private JFrame frame = new JFrame("Moodish App");

	private ArrayList<User> connectedFriendList = new ArrayList<User>();
	private User user;

	private MainPanel mainPanel;
	private LogWindow simpleLogin;
	private Sentinel sentinel;

	private MoodishMessage moodishMessage = new MoodishMessage(this);
	private MessageHendler hendler;

	public static final int WINDOW_WIDTH = 700;
	public static final int WINDOW_HEIGHT = 600;
	
	private ClientComm comm;
	
	public MoodishClientGui(ClientComm comm) {
		setGui();
		this.setComm(comm);
		setUpButton();
		frame.addMouseListener(new RepaintListener(this));
		frame.getContentPane().add(simpleLogin);
		frame.setSize(new Dimension(700, 600));
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		new Painter().start();
	}

	public void executeClient() {
		frame.setVisible(true);
	}

	private void setUpButton() {
		simpleLogin = new LogWindow(this);
		sentinel = new Sentinel(this);
		simpleLogin.getLogin().addActionListener(sentinel);
	}

	public MainPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(MainPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	public void changeView(JPanel panel) {
		frame.getContentPane().removeAll();
		frame.getContentPane().add(panel);
		frame.validate();
		frame.repaint();
	}

	public ArrayList<User> getConnectedFriendList() {
		return connectedFriendList;
	}

	public void setConnectedFriendList(ArrayList<User> connectedFriendList) {
		this.connectedFriendList = connectedFriendList;
	}



	public void gotoMainPane() {
		MainPanel mainPanel = new MainPanel(this);
		setMainPanel(mainPanel);
		setJMenu(mainPanel);
		changeView(mainPanel);
	}

	private void setGui() {

		try {
			UIManager.setLookAndFeel(UIManager.getInstalledLookAndFeels()[1]
					.getClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	private void setJMenu(JPanel panel) {
		JMenu options = new JMenu("Options");
		JMenu help = new JMenu("Help");

		JMenuItem exit = new JMenuItem("Exit");
		JMenuItem Settings = new JMenuItem("Settings");

		options.add(Settings);
		options.addSeparator();
		options.add(exit);

		JMenuBar menuBar = new JMenuBar();
		menuBar.add(options);
		menuBar.add(help);

		exit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		panel.add(menuBar, BorderLayout.NORTH);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public JFrame getFrame() {
		return frame;
	}

	public Sentinel getSentinel() {
		return sentinel;
	}

	public MoodishMessage getMoodishMessage() {
		return moodishMessage;
	}

	public MessageHendler getHendler() {
		return hendler;
	}

	public void setHendler(MessageHendler hendler) {
		this.hendler = hendler;
	}

	/**
	 * @return the simpleLogin
	 */
	public LogWindow getSimpleLogin() {
		return simpleLogin;
	}

	/**
	 * @param simpleLogin
	 *            the simpleLogin to set
	 */
	public void setSimpleLogin(LogWindow simpleLogin) {
		this.simpleLogin = simpleLogin;
	}

	public ClientComm getComm() {
		return comm;
	}

	public void setComm(ClientComm comm) {
		this.comm = comm;
	}
	
	private class Painter extends Thread {
		
		@Override
		public void run() {
			super.run();
			
			while(getComm().hasNextMessage()){
				getFrame().validate();
				getFrame().repaint();
			}
		}
	}
}
